﻿using Sidvall.FileSystemManager.Data;
using Sidvall.Serialization;

namespace Sidvall.FileSystemManager.Test
{
    public static class Program
    {
        #region Public Members

        public static void Main(string[] args)
        {
            string[] fileSearchPatterns;

            RegisterTypes();
            try
            {
                if ((args == null) || (args.Length < 2))
                {
                    System.Console.WriteLine("Usage 1:");
                    System.Console.WriteLine("Sidvall.FileSystemManager.Test.exe rename <path to root folder> <oldName> <newName> <optional include patterns>");
                    System.Console.WriteLine("Default include patterns = " + CodeManager.SearchPatterns);
                    System.Console.WriteLine("Usage 2:");
                    System.Console.WriteLine("Sidvall.FileSystemManager.Test.exe clean <path to root folder>");
                    System.Console.WriteLine("Usage 3:");
                    System.Console.WriteLine("Sidvall.FileSystemManager.Test.exe search <path to root folder> <search string> <optional include patterns>");
                    System.Console.WriteLine("Default include patterns = " + CodeManager.SearchPatterns);
                    System.Console.ReadLine();
                }
                else
                {
                    switch (args[0].ToUpperInvariant())
                    {
                        case "CLEAN":
                            Sidvall.FileSystemManager.CodeManager.CleanAsync(args[1], true, true, true, true, CodeManager.DefaultRetryCount).Wait();
                            break;
                        case "RENAME":
                            if (args.Length >= 5)
                                fileSearchPatterns = args[4].Split(CodeManager.SearchPatternSeparator);
                            else
                                fileSearchPatterns = CodeManager.SearchPatterns.Split(CodeManager.SearchPatternSeparator);
                            var nameItems = new NameItemCollection();
                            nameItems.Add(args[2], args[3]);
                            Sidvall.FileSystemManager.CodeManager.RenameAsync(args[1], nameItems, fileSearchPatterns, true, true, true, true, true).Wait();
                            break;
                        case "SEARCH":
                            if (args.Length >= 4)
                                fileSearchPatterns = args[3].Split(CodeManager.SearchPatternSeparator);
                            else
                                fileSearchPatterns = CodeManager.SearchPatterns.Split(CodeManager.SearchPatternSeparator);
                            var result = Sidvall.FileSystemManager.CodeManager.SearchAsync(args[1], fileSearchPatterns, args[2], true, false, true, true, false).Result;
                            System.Console.WriteLine();
                            System.Console.WriteLine("RESULT");
                            foreach (var item in result.PathNames.Items)
                                System.Console.WriteLine(item.Name);
                            break;
                        default:
                            break;
                    }
                }
                System.Console.WriteLine();
                System.Console.WriteLine("Done.");
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(Sidvall.SystemContext.Current.SystemExceptionManager.GetMessage(ex));
            }
            System.Console.ReadLine();
        }

        #endregion
        #region Private Members

        #region RegisterTypes

        private static void RegisterTypes()
        {
            // Framework
            var authorizationProvider = new Sidvall.Security.PublicAuthorizationProvider();
            Sidvall.SystemContext.Current = new Sidvall.SystemContext()
            {
                AuthorizationManager = new Sidvall.Security.AuthorizationManager(authorizationProvider),
                //AuthenticationManager = new Sidvall.Security.AuthenticationManager(new Sidvall.Security.AuthenticationProvider()),
                //CacheManager = new Sidvall.Caching.CacheManager(new Sidvall.Caching.MemoryCache()),
                //ClaimManager = new Sidvall.Security.ClaimManager(new Sidvall.Security.ClaimsPrincipalManager()),
                //CodeManager = new Sidvall.Reflection.CodeManager(new Sidvall.Reflection.Code()),
                UserExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.UserExceptionProvider()),
                SystemExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.SystemExceptionProvider()),
                //HttpClientFactory = new Sidvall.Net.HttpClientFactory(),
                //LogManager = new Sidvall.Logging.LogManager(new Sidvall.Logging.EL.Log()),
                //SmtpClientManager = new Sidvall.Net.Mail.SmtpClientManager(new Sidvall.Net.Mail.SmtpClient()),
                //SoapClientFactory = new Sidvall.Net.SoapClientFactory(),
                SystemStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem()),
                //TransactionScopeFactory = new Sidvall.Data.TransactionScopeFactory(),
                TypeCache = new Sidvall.Reflection.TypeCache(),
                UserManager = new Sidvall.Security.UserManager(authorizationProvider),
                //UserStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.Storage.LocalStorage()),

                // Client
                PrincipalContextManager = new Sidvall.Security.PrincipalContextManager(new Sidvall.Security.PrincipalContextProvider()),
                //PrincipalManager = new Sidvall.Security.PrincipalManager(new Sidvall.Security.PrincipalProvider()),

                // Host specific
                ResourceManager = new Sidvall.Resources.ResourceManager(new Sidvall.FileSystemManager.Test.Resources.ResourceProvider()),
                //SettingsManager = new Sidvall.Configuration.SettingsManager(new Sidvall.FileSystemManager.Test.Configuration.ApplicationSettings()),
            };
        }

        #endregion

        #endregion
    }
}
